package com.heima.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.heima.article.job.XxlJobTast;
import com.heima.article.service.ApArticleService;
import com.heima.article.service.HotArticleService;
import com.heima.common.constants.ArticleConstants;
import com.heima.common.redis.CacheService;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.vos.HotArticleVo;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class HotArticleServiceImpl implements HotArticleService {
    @Autowired
    private ApArticleService apArticleService;
    @Autowired
    private CacheService cacheService;


    /**
     * 计算热点文章
     */
    public void computeHotArticle() {
        //1.获取前五天的文章
        Date date = DateTime.now().minusDays(5).toDate();
        List<ApArticle> apArticles = apArticleService.findArticleListByLast5days(date);
        //2.计算分值
        List<HotArticleVo> hotArticleVos = apArticles.stream().map(apArticle -> {
            HotArticleVo hotArticleVo = new HotArticleVo();
            BeanUtils.copyProperties(apArticle, hotArticleVo);
            hotArticleVo.setScore(computeScore(apArticle));
            return hotArticleVo;
        }).collect(Collectors.toList());
        //存入缓存
        List<Integer> channelIds = apArticles.stream().map(ApArticle::getChannelId).distinct().collect(Collectors.toList());
        for (Integer channelId : channelIds) {
            List<HotArticleVo> channelList = hotArticleVos.stream().filter(article -> article.getChannelId().equals(channelId)).sorted((o1, o2) -> o2.getScore() - o1.getScore()).limit(40).collect(Collectors.toList());
            cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + channelId, JSON.toJSONString(channelList));
        }
        List<HotArticleVo> allList = hotArticleVos.stream().sorted((o1, o2) -> o2.getScore() - o1.getScore()).limit(40).collect(Collectors.toList());
        cacheService.set(ArticleConstants.HOT_ARTICLE_FIRST_PAGE + ArticleConstants.DEFAULT_TAG, JSON.toJSONString(allList));
    }

    /**
     * 文章算分
     * @param apArticle
     * @return
     */
    public Integer computeScore(ApArticle apArticle) {
        Integer score = 0;
        if (apArticle == null) {
            return score;
        }
        if (apArticle.getLikes() != null) {
            score += apArticle.getLikes() * ArticleConstants.HOT_ARTICLE_LIKE_WEIGHT;
        }
        if (apArticle.getCollection() != null) {
            score += apArticle.getCollection() * ArticleConstants.HOT_ARTICLE_COLLECTION_WEIGHT;
        }
        if (apArticle.getComment() != null) {
            score += apArticle.getComment() * ArticleConstants.HOT_ARTICLE_COMMENT_WEIGHT;
        }
        if (apArticle.getViews() != null) {
            score += apArticle.getViews();
        }
        return score;
    }
}
